package com.xzq.erp.config;

import com.xzq.erp.properties.KeyStoreProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.crypto.encrypt.KeyStoreKeyFactory;

import java.security.KeyPair;

@Configuration
@EnableConfigurationProperties(KeyStoreProperties.class)
public class AuthConfig {

    @Bean
    public KeyStoreProperties keyProperties() {
        return new KeyStoreProperties();
    }

    @Bean
    public KeyPair keyPair(KeyStoreProperties keyProperties) {
        // 获取秘钥工厂
        KeyStoreKeyFactory keyStoreKeyFactory =
                new KeyStoreKeyFactory(
                        new ClassPathResource(keyProperties.getLocation()),
                        keyProperties.getPassword().toCharArray());
        //读取钥匙对
        return keyStoreKeyFactory.getKeyPair(
                keyProperties.getAlias(),
                keyProperties.getSecret().toCharArray());
    }
}